package system;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import object.obj3D.enemy.Character;

import com.game.R;

public class MiniMap {
	// ------------------------------------------------------------
	// / Variable Declare
	// ------------------------------------------------------------
	private float m_sizeWidth =Device.GetWidth()/5;
	private float m_sizeHeight = Device.GetWidth()/5;
	private float m_transX = (Device.GetWidth() - m_sizeWidth) / 2 - 4;
	private float m_transY = (Device.GetHeight() - m_sizeHeight) / 2 - 24;
	private float m_ratioW;
	private float m_ratioH;
	private float m_beta = 0;
	private GL10 gl = Graphics.GetGraphics();
	private List<Character> m_charList = null;
	private float m_maxDistance = 30;

	private Texture m_texture = new Texture();
	private TexSquare m_texEnemy;
	private TexSquare m_texMap;
	private int m_mapID;
	private int m_arrowID;
	private int m_mapResID = R.drawable.map_minimap_bg;
	private int m_arrowResID = R.drawable.map_minimap;
	private int m_enemyID;

	// ------------------------------------------------------------
	// / Constructor
	// ------------------------------------------------------------
	public MiniMap() {
		m_ratioW = (m_sizeWidth / 2) / (m_maxDistance*1.1f);
		m_ratioH = (m_sizeHeight / 2) / (m_maxDistance*1.1f);
		this.Init();
	}

	// ------------------------------------------------------------
	// / Initialize
	// ------------------------------------------------------------
	public void Init() {
		Device.DisableScaleFactor();
		m_mapID = m_texture.initTexture(m_mapResID);
		m_enemyID = m_texture.initTexture(R.drawable.map_enemy);
		m_arrowID = m_texture.initTexture(m_arrowResID);
		m_texMap = new TexSquare(m_sizeWidth, m_sizeHeight);
		m_texEnemy = new TexSquare(5f, 5f);
		Device.EnableScaleFactor();
	}

	public void Update(List<Character> charList, float beta) {
		this.setCharList(charList);
		this.setBeta(beta);
	}

	public void Render() {
		gl.glPushMatrix();
			gl.glLoadIdentity();
			gl.glTranslatef(m_transX, m_transY, 0);
			gl.glPushMatrix();
				gl.glRotatef(-m_beta, 0, 0, 1);
				m_texture.loadTexture(m_mapID);
				m_texMap.Render();
			gl.glPopMatrix();
			
			m_texture.loadTexture(m_arrowID);
			m_texMap.Render();
			
			// Draw enemy position
			if (m_charList != null){
				for (int i = 0; i < m_charList.size(); i++) {
					if (m_charList.get(i).GetDistance() < m_maxDistance) {
						this.drawEnemy(m_charList.get(i));
					}
				}
			}
		gl.glPopMatrix();
	}

	// ------------------------------------------------------------
	// / drawMiniMap
	// ------------------------------------------------------------
	private void drawEnemy(Character m_character) {
		float x, z;
		x = m_character.GetPosition().GetX() * m_ratioW;
		z = m_character.GetPosition().GetZ() * m_ratioH;
		gl.glPushMatrix();
		gl.glRotatef(-m_beta, 0, 0, 1);
		gl.glTranslatef(-x, z, 0);
		m_texture.loadTexture(m_enemyID);
		m_texEnemy.Render();
		gl.glPopMatrix();
	}

	// ------------------------------------------------------------
	// / Getter and setter function
	// ------------------------------------------------------------
	public void setSizeWidth(float m_sizeWidth) {
		this.m_sizeWidth = m_sizeWidth;
		m_ratioW = (m_sizeWidth / 2) / (m_maxDistance*1.1f);
		m_texMap = new TexSquare(m_sizeWidth, m_sizeHeight);
	}

	public float getSizeWidth() {
		return m_sizeWidth;
	}

	public void setSizeHeight(float m_sizeHeight) {
		this.m_sizeHeight = m_sizeHeight;
		m_ratioW = (m_sizeHeight / 2) / (m_maxDistance*1.1f);
		m_texMap = new TexSquare(m_sizeWidth, m_sizeHeight);
	}

	public float getSizeHeight() {
		return m_sizeHeight;
	}

	public void setTransX(float m_transX) {
		this.m_transX = m_transX;
	}

	public float getTransX() {
		return m_transX;
	}

	public void setTransY(float m_transY) {
		this.m_transY = m_transY;
	}

	public float getTransY() {
		return m_transY;
	}

	public void setMapResID(int m_mapResID) {
		this.m_mapResID = m_mapResID;
		m_mapID = m_texture.initTexture(m_mapResID);
	}

	public int getMapResID() {
		return m_mapResID;
	}

	public void setBeta(float m_beta) {
		this.m_beta = m_beta;
	}

	public float getBeta() {
		return m_beta;
	}

	public void setCharList(List<Character> m_charList) {
		this.m_charList = m_charList;
	}

	public List<Character> getCharList() {
		return m_charList;
	}

	public void setMaxDistance(float m_maxDistance) {
		this.m_maxDistance = m_maxDistance;
		m_ratioW = (m_sizeWidth / 2) / (m_maxDistance*1.1f);
		m_ratioH = (m_sizeHeight / 2) / (m_maxDistance*1.1f);
	}

	public float getMaxDistance() {
		return m_maxDistance;
	}
}
